/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "SkyLight.h"
#include "CameraObject.h"

namespace PQEngine
{
	SkyLight::SkyLight(SceneObject* sceneObject)
		: _sceneObject(sceneObject),_direction(Ogre::Vector3::ZERO),
		_bodyColor(Ogre::ColourValue::White),_lightColor(Ogre::ColourValue::White),
		_diffuseMultiplier(Ogre::ColourValue(1, 1, 0.9)),_specularMultiplier(Ogre::ColourValue(1, 1, 1)),
		_ambientMultiplier(Ogre::ColourValue(0.2, 0.2, 0.2))
	{
		Ogre::String lightName="SkyLight"+Ogre::StringConverter::toString(getId());
		_mainLight=_sceneObject->createLightObject(LIGHT_TYPE_DIRECTIONAL);
		_mainLight->setType(LIGHT_TYPE_DIRECTIONAL);
		_sceneObject->getSceneManager()->getRenderQueue()->getQueueGroup(SCENE_RENDER_QUEUE_SUN)->setShadowsEnabled(false);

		//Our parent node is camera's scene node,so the sky light will always follow us
		_sceneNode=_sceneObject->createCameraFollowSceneNode();
		

	}

	void SkyLight::setFarRadius(Ogre::Real radius)
	{
		_radius=radius;
	}

	void SkyLight::update(const Ogre::Vector3& direction,const Ogre::ColourValue &lightColor,const Ogre::ColourValue &bodyColor)
    {
		setLightDirection(direction);
		setLightColor(lightColor);
		setBodyColor(bodyColor);

		setFarRadius((_sceneObject->getCamera()->getFarClipDistance() + _sceneObject->getCamera()->getNearClipDistance()) / 2);

    }

	void SkyLight::setLightColor(const Ogre::ColourValue &color)
	{
		_mainLight->setDiffuseColor(color*_diffuseMultiplier);
		_mainLight->setSpecularColor(color*_specularMultiplier);
	}

	void SkyLight::setBodyColor(const Ogre::ColourValue &color)
	{
		_bodyColor=color;
	}

	void SkyLight::setLightDirection(const Ogre::Vector3& direction)
    {
		_direction=direction;
		if(_mainLight){
			_mainLight->setDirection(_sceneNode->_getDerivedOrientation()*_direction);
		}
    }

}
